package lumis.portal.presentation;


import javax.servlet.jsp.el.ELException;
import javax.servlet.jsp.el.FunctionMapper;

import lumis.portal.PortalContext;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.cache.PortalCache;
import lumis.portal.el.LumisExpressionEvaluatorImpl;

/**
 * Responsible for returning the loading message for client side AJAX calls.
 *
 * @version $Revision: 12838 $ $Date: 2011-04-18 19:40:40 -0300 (Mon, 18 Apr 2011) $
 * @since 4.2.0
 */
public class LoadingMessage
{
	protected final static LumisExpressionEvaluatorImpl evaluator = new LumisExpressionEvaluatorImpl();
	protected final static FunctionMapper localizationFunctionMapper = new LocalizationFunctionMapper();
	protected static PortalCache<String> loadingMessages = new PortalCache<String>("lumis.portal.presentation.LoadingMessage");
	
	/**
	 * Returns the loading message based on the current locale of the user.
	 * 
	 * @return
	 * @since 4.2.0
	 */
	public static String getLoadingMessage()
	{
		String currentLocaleId = SessionConfig.getCurrentSessionConfig().getLocale().toString();
		String loadingMessage = loadingMessages.get(currentLocaleId);
		if(loadingMessage == null)
		{
			try
			{
				loadingMessage = PortalContext.getLoadingHtmlMessage();
				if(loadingMessage.contains("${"))
					loadingMessage = (String)evaluator.evaluate(loadingMessage, String.class, null, localizationFunctionMapper);
			}
			catch (ELException e)
			{
				throw new RuntimeException("Error evaluating loading message:" + loadingMessage, e);
			}
			loadingMessages.put(currentLocaleId, loadingMessage);
		}

		return loadingMessage;
	}
}
